package com.example.learn.controller;


import com.example.learn.domain.Optionaler;
import com.example.learn.entity.Account;
import com.example.learn.entity.User;
import com.example.learn.service.UserService;
import com.example.learn.token.CurrentUser;
import com.example.learn.token.UserLoginToken;
import com.example.learn.utile.JwtUtil;
import com.example.learn.token.PassToken;
import com.example.learn.utile.Response.BaseResponse;
import com.example.learn.utile.Response.ObjectRestResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/user")
public class UserController {
    private static final Logger LOG = LoggerFactory.getLogger(UserController.class);
    @Autowired
    UserService userService;

    //登录
    @PassToken
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public  BaseResponse index(@RequestParam  String name,String password){
        User user=  userService.login(name,password);
        if(user==null){
        return new ObjectRestResponse().data("false");
        }
        String token = JwtUtil.getToken(user);
        user.setToken(token);

        return new ObjectRestResponse().data(user);
    }

    @RequestMapping(value = "/addUser",method = RequestMethod.POST)
    public BaseResponse addUser(  @RequestBody User user){
     boolean bool=   userService.addUser(user);
        if(bool){
            return new ObjectRestResponse().data("ok");
        }
        return new ObjectRestResponse().data("用户名已经被注册");
    }
    @UserLoginToken
    @RequestMapping("/user2")
    public User getUser(){
        User user=   userService.getUser2();

        return user;
    }
    @RequestMapping("/getName")
    public BaseResponse getName(String name){
        boolean bool=   userService.getName(name);
        if(bool){
            return new ObjectRestResponse().data("ok");
        }
        return new ObjectRestResponse().data("用户名已经被注册");
    }
    @UserLoginToken
    @RequestMapping(value = "/updateUser",method = RequestMethod.POST)
    public BaseResponse updateUser( @RequestBody User user){
        boolean bool=   userService.updateUser(user);
        if(bool){
            return new ObjectRestResponse().data("ok");
        }
        return new ObjectRestResponse().data("更新失败");
    }
    @UserLoginToken
    @RequestMapping(value = "/getUserByName",method = RequestMethod.GET)
    public BaseResponse getUserByName(  String name){
        User user=  userService.getUserByName(name);
        if(user!=null){
            return new ObjectRestResponse().data(user);
        }
        return new ObjectRestResponse().data("false");
    }
    @UserLoginToken
    @RequestMapping(value = "/addfamily",method = RequestMethod.POST)
    public BaseResponse addfamily( @RequestParam String name,@CurrentUser User user){
        String string=  userService.addfamily(name,user);

        return new ObjectRestResponse().data(string);
    }
    @UserLoginToken
    @RequestMapping(value = "/getFamilyName",method = RequestMethod.GET)
    public BaseResponse getFamilyName(@CurrentUser User user){
        List<User> users=  userService.getFamilyName(user);

        return new ObjectRestResponse().data(users);
    }
    @UserLoginToken
    @RequestMapping(value = "/deleteFamilyName",method = RequestMethod.POST)
    public BaseResponse deleteFamilyName(@RequestParam String name,@CurrentUser User user){
        String message=  userService.deleteFamilyName(name,user);

        return new ObjectRestResponse().data(message);
    }
    @UserLoginToken
    @RequestMapping(value = "/getoptions",method = RequestMethod.GET)
    public BaseResponse getoptions(@CurrentUser User user){
        List<Optionaler> users=  userService.getoptions(user);

        return new ObjectRestResponse().data(users);
    }

}
